<?php

namespace App\Handlers;

use App\Facades\DeviceService;
use App\Facades\UserManager;
use App\Models\UserInfo;
use Cache;
use Exception;

class InnerHandler
{
	/**
	 * 用户给内部监听发指令时，此方法把指令发送到设备
	 * @param   $serv
	 * @param   $fd
	 * @param   $from_id
	 * @param   $data
	 * @return           
	 */
	public function onReceive($serv, $fd, $from_id, $data)
	{
		try{
			$dataArray = explode(' ', $data);
			$cmd = $dataArray[0];

			if($cmd == 'send_clock')
			{
				$deviceFd = $dataArray[3];
				$cmd = DeviceService::getSendClockData();
			}else{

				$num = $dataArray[1];
				$deviceCode = $dataArray[2];
				$cmd = DeviceService::getCmd($cmd, $num, $deviceCode);
				if(!$cmd){
					return ;
				}
				$deviceFd = Cache::get($deviceCode.'fd');
			}

			$serv->send($deviceFd, $cmd);
		}catch(Exception $e){
			logger(['InnerHandler@onReceive' => [
				'line' => $e->getLine(),
				'file' => $e->getFile(),
				'message' => $e->getMessage(),
				]]);
		}
	}
}